package com.jiazhong.spring.ioc.xml;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {


    // 获取到IoC容器中的对象
    private static void a() {
        // 1. 加载xml 产生一个容器IoC
        ApplicationContext context
                = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        // 2. 从IoC容器中获取到指定对象
        Object obj = context.getBean("da1");
        System.out.println(obj);
    }

    private static void b() {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        Object obj = context.getBean("da2");
        System.out.println(obj);
    }

    private static void c() {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        Object obj = context.getBean("da3");
        System.out.println(obj);
    }

    private static void d() {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        Object obj = context.getBean("da4");
        System.out.println(obj);
    }


    private static void e() {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        Object obj1 = context.getBean("db1");
        Object obj2 = context.getBean("db2");
        Object obj3 = context.getBean("db3");
        Object obj4 = context.getBean("db4");
        System.out.println(obj1);
        System.out.println(obj2);
        System.out.println(obj3);
        System.out.println(obj4);
    }


    private static void f() {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        Object obj1 = context.getBean("dc1");
        Object obj2 = context.getBean("dc2");
        Object obj3 = context.getBean("dc3");
        System.out.println(obj1);
        System.out.println(obj2);
        System.out.println(obj3);
    }

    private static void g() {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        // 获取到容器中的对象
        // byName方式
        DemoA da3 = (DemoA) context.getBean("da3");
        // byType方式 必须只有一个，否则会报错
        // DemoC dc = context.getBean(DemoC.class);
        // byType+byName
        DemoC dc = context.getBean("dc2", DemoC.class);
        System.out.println(da3);
        System.out.println(dc);
    }

    public static void main(String[] args) {
        g();

    }
}
